<?php
/**
 * 生成guid
 * @return string
 */
function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45);// "-"
    $uuid =
        substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12);
    return substr(md5($uuid), 8,10);
}



/**
 *
 * @param string $url  地址
 * @param Array|String $params  array or string
 * @param String $mode post or get
 * @param Boolean $xml true or false
 * @return String
 */
function sendCurl($url, $params, $mode = 'post', $xml = false)
{
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_TIMEOUT, 30);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    if ($mode == 'post')
    {
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($curlHandle, CURLOPT_POST, true);
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, '20');
        curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制使用哪个版本
        if ($xml === TRUE)
        {
            $header[] = "Content-type: text/xml"; //定义content-type为xml
            curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $params);
        } else
        {
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, http_build_query($params));
        }
    } else
    {
        $url .= ( strpos($url, '?') === false ? '?' : '&') . http_build_query($params);
    }
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    if (substr($url, 0, 5) == 'https')
    {
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
    }

    $result = curl_exec($curlHandle);
    $curl_errno = curl_errno($curlHandle);
    $curl_error = curl_error($curlHandle);
    if ($curl_errno > 0)
    {
        //Log::info("cURL Error ($curl_errno): $curl_error\n");
    }
    curl_close($curlHandle);
    return $result;
}